<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class AdminShopSpePrice extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_spe_price';

    protected static function init()
    {
        //TODO:初始化内容
    }


    /**
     * @var array
     * 查询器
     */
    protected $append = [
        'spe_array_text',
        'spe_name_text'
    ];
    /**
     * @param $value
     * @param $data
     * @return mixed
     * get spename
     */
    public function getSpeNameTextAttr($value,$data){
        $pec_id    = explode('-',$data['spe_id_1']);
        $spe_names = Db::name('longbing_card_shop_spe')->where(['status'=>1])->where('id','IN',$pec_id)->select()->toArray();
        if(!empty($spe_names)){
            foreach ($spe_names as $value){
                $spe_name[] = $value['title'];
            }
            return  implode('-',$spe_name);
        }

    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * get spearray
     */
    public function getSpeArrayTextAttr($value,$data){
        return explode('-',$data['spe_id_1']);
    }

    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取商品多规格价格库存
     */
    public function goodsSpePrice($dis){
        $data = $this->where($dis)->select()->toArray();
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * 添加商品规格
     */

    public function goodsSpePriceAdd($data){
        $data['create_time'] = time();
        $data['status']      = 1;
        $res = $this->insert($data);
        return $res;
    }


    /**
     * @param $data
     * @return int|string
     * 添加商品规格
     */

    public function goodsSpePriceSaveAll($data){
        $res = $this->saveAll($data);
        return $res;
    }

    /**
     * @param $data
     * @return int|string
     * 编辑啊商品规格
     */

    public function goodsSpePriceUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;
    }

    /**
     * @param $dis
     * 根据条件获取id
     */

    public function goodsSpePriceId($dis){
        $data = $this->where($dis)->column('id');
        return $data;

    }


    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取商品多规格价格库存
     */
    public function singeSpePrice($dis){
        $data = $this->where($dis)->find();
        return !empty($data)?$data->toArray():$data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-25 16:13
     * @功能说明:获取售罄的商品id
     */
    public function getSellOut($uniacid,$type=0){

        $dis[] = ['uniacid','=', $uniacid];

        $dis[] = ['status','=', 1];

        $all_goods = $this->where($dis)->column('goods_id');

        $dis[] = ['stock','>',0];

        $diff_goods = $this->where($dis)->column('goods_id');

        if($type==1){

            return $diff_goods;
        }

        $data = array_diff($all_goods,$diff_goods);

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-26 09:53
     * @功能说明:获取商品库存
     */
    public function getGoodsStock($goods_id){

        $dis = [

            'goods_id' => $goods_id,

            'status'   => 1
        ];

        $stock = $this->where($dis)->sum('stock');

        return is_numeric($stock)?$stock:0;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-19 18:15
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }





}